//
//  Extra3ViewController.m
//  ToucheTouche
//
//  Created by polytech polytech on 11/01/12.
//  Copyright (c) 2012 polytech. All rights reserved.
//

#import "Extra3ViewController.h"
#import "ModeGameViewController.h"
#import "GameAventureViewController.h"
#import "Level.h"

@implementation Extra3ViewController
@synthesize button;
@synthesize imageView,temps,score,random,reaction,click,image;
@synthesize gameAventureViewController;
@synthesize delegate;
@synthesize alertLevel,life;


- (IBAction)actionButton{
    click=YES;
    [self countScore];
    [self stopTimer];
}
-(void) changeImage{
    NSString *fileName = [NSString stringWithFormat: @"imageTest2.png"];
    imageView.image = [UIImage imageNamed:fileName];
    image=YES;
    [self countTime];
}

- (void) countScore{
    if(image== NO){
        score=0;
        life=0;
    }else if(reaction<=0.3){
        score=300;
        [[Level sharedLevel] addLife:10 :2];
        life=10;
    }else if(reaction <=0.4){
        [[Level sharedLevel] addLife:10 :2];
        life=10;
        score =200;
    }else if(reaction <=0.5){
        [[Level sharedLevel] addLife:5 :1];
        score =100;
        life=5;
    }else if(reaction <=0.6){
        [[Level sharedLevel] addLife:5 :1];
        score=50;
        life=5;
    }else{
        score=10;
        life=0;
    }
    [[Level sharedLevel] initExtraScore:score];

}
/* methode timer */
- (void)timer:(float) tmp {
    temps = tmp;
    chrono = [[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES] retain];
    // Affiche le compteur
  //  labelTmp.text = [NSString stringWithFormat:@"%d", temps];
}

- (void) countTime{
    reaction = 0;
    chrono = [[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateCountTime:) userInfo:nil repeats:YES] retain];
}
- (void) updateCountTime:(NSTimer *) aTimer{
    reaction+=0.05;
}

/* methode affichant le chrono */
- (void) updateTimer: (NSTimer *) aTimer {
    temps-=0.5;
    if(temps == 5-random){
        [self changeImage ];
    }
    // Arrète le NSTimer lorsque le compte à rebour est à zéro
    if ( temps == 0 && click==NO) {
        [self stopTimer];
    }
}

/* methode arret timer */
- (void)stopTimer {
    [chrono invalidate];
    chrono = nil;
    
    // Affiche l'alerte avec le score
    alertLevel = [[UIAlertView alloc] initWithTitle:@"Résultat de l'extra" message:[NSString stringWithFormat:@"Score : %.0f \nVie gagnée : %d\nPassage au niveau 4", [[Level sharedLevel] floatExtraScore],life] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertLevel show];
    [alertLevel release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  {
    if (alertView == alertLevel) {
        if (buttonIndex == 0) {
            
            // ajout du score de l'extra au score global
            [self.gameAventureViewController addScoreNextExtra];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"AddScoreNextExtraNotification" object:nil];
            
            // mise a jour du nouveau niveau [bonnes valeurs]
            // init : score, score to add, score for extra, life, life to remove, lifeprogress, lifeprogress to remove, time level, time level to remove, current level, time image, background, image
            //[[Level sharedLevel] initLevel:[[Level sharedLevel] floatScore] :20 :1100 :[[Level sharedLevel] floatLife] :5 :[[Level sharedLevel] floatLifeProgress] :0.05 :31 :1 :4 :0.6: @"backgroundLevel4": @"imageLevel4"];
            
            // POUR LES TESTS
            [[Level sharedLevel] initLevel:[[Level sharedLevel] floatScore] :20 :20 :[[Level sharedLevel] floatLife] :5 :[[Level sharedLevel] floatLifeProgress] :0.05 :10 :1 :4 :0.6: @"": @"imageTest.png"];
            
            
            
            NSLog(@"Score actuel : %.0f", [[Level sharedLevel] floatScore]);
            NSLog(@"Score a ajouter : %.0f", [[Level sharedLevel] floatScoreToAdd]);
            NSLog(@"vie actuelle : %.0f", [[Level sharedLevel] floatLife]);
            NSLog(@"vie a enlever : %.0f", [[Level sharedLevel] floatLifeToRemove]);
            NSLog(@"lifeprogress : %.0f", [[Level sharedLevel] floatLifeProgress]);
            NSLog(@"lifeprogress to remove : %.2f", [[Level sharedLevel] floatLifeProgressToRemove]);
            NSLog(@"temps du niveau : %d", [[Level sharedLevel] intTimeLevel]);
            NSLog(@"numero niveau : %d", [[Level sharedLevel] intCurrentLevel]);
            
            
            [self.gameAventureViewController initLabel]; // init des labels
            [[NSNotificationCenter defaultCenter] postNotificationName:@"InitLabelNotification" object:nil];
            [self.gameAventureViewController timerImage]; // deplacement image
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TimerNotification" object:nil];
            [self.gameAventureViewController timerLevel:[[Level sharedLevel] intTimeLevel]]; // temps du niveau
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TimerLevelNotification" object:nil];
            
            
            
            
            // Redirection vers le jeu
            [self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [self dismissModalViewControllerAnimated:YES];
            
        }
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *fileName = [NSString stringWithFormat: @"imageTest.png"];
    imageView.image = [UIImage imageNamed:fileName];
    reaction=0;
    random = ((arc4random() % 5)+2);
    random = random/2;
    click=NO;
    image=NO;
    score=0;
    life=0;
    [[Level sharedLevel] initExtraScore:score];
    [self timer:5];
}

- (void)viewDidUnload
{
    [self setButton:nil];
    [self setImageView:nil];
    [super viewDidUnload];
    self.temps=0;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
      return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void)dealloc {
    [button release];
    [imageView release];
    [super dealloc];
}
@end
